// pages/lesson-record/index.js
const handler = require("../../utils/function.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    spinShow: true,
    recordData:[],
    current_page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = wx.getStorageSync('user_info').id;
    var user_type = wx.getStorageSync('user_info').user_type;
    this.initData(id, user_type);
  },
  initData: function (id, user_type) {
    var para = {
      user_id: id,
      user_type: user_type,
      current_page: this.data.current_page
    };
    var that = this;
    that.setData({
      user_type: user_type
    })
    handler.requestData("lessonRecord", para, function (res) {
      if (res.success == true) {
        that.setData({
          spinShow: false,
          recordData: res.data.data,
          total_page: res.data.total
        })
        // console.log(that.data.recordData)
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });

  },
  // 获取数据
  get_data() {
    var that = this;
    var i = 0;
    var time = setInterval(() => {
      if (i < 5) { i++; console.log() }
      else { clearInterval(time); that.setData({ spinShow: false }) }
    }, 1000)
  },
  // 页面跳转
  handler_next_page(e) {
    handler.intoPageHandler(e.currentTarget.dataset.url);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    var id = wx.getStorageSync('user_info').id;
    var user_type = wx.getStorageSync('user_info').user_type;
    this.initData(id, user_type);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉加载更多
    var current_page = this.data.current_page;
    var total_page = this.data.total_page;
    if (total_page < current_page * 4) {
      console.log("已经是全部数据");
    } else {
      this.setData({
        current_page: current_page + 1
      })
      wx.showNavigationBarLoading() //在标题栏中显示加载
      var id = wx.getStorageSync('user_info').id;
      var user_type = wx.getStorageSync('user_info').user_type;
      this.initData(id, user_type);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})